// pages/attendanceDepartDay/attendanceDepartDay.js
const app = getApp();
import CommonService from '../../../utils/service/commonService.js';
const util = require('../../../utils/util');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //当前月份
    currentMonth: new Date().getMonth() + 1,
    //查询的日期
    currentDate:util.timeFormatDay(new Date()),
    //明天
    tomorrow:null,
    //考勤数据
    clockInInfo:null,
    //环的数值
    clockNum:0,
    //当前用户
    user:null,
    //出勤率
    rateList:[],
    //权限
    authority: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let user=app.globalData.userInfo;
    let authority=app.globalData.authority;

    this.setData({
      user: user,
      authority: authority
    })

    //如果是员工登录，跳转个人统计页面
    if(authority.manage && authority.manage.userCard && authority.manage.userCard.staff){
      //查询人员信息
      this.getUserInfo();
    }
  },

  //查询人员信息
  getUserInfo(){
    let param = {
      isAuth: false,
      className: 'user',
      data: {
        id:this.data.user.id,
        selectFields: ['id','imei','nickName']
      }
    }

    CommonService.goFindList(param).then(res => {
      if(res.data.data && res.data.data.result && res.data.data.result.length) {
        let result=res.data.data.result[0];
        console.log('员工信息');
        console.log(result);
        
        let obj={
          imei: result.imei ? result.imei : null,
          nickName: result.nickName
        }
    
        wx.redirectTo({
          url: "/packageA/pages/attendancePersonDay/attendancePersonDay?paramObj="+JSON.stringify(obj)
        })  
      }
    }).catch((error) => {
    })
  },

  //获取选择日期
  bindgetdate(e) {
    let authority = this.data.authority;
    
    //非员工查部门
    if(authority.manage && authority.manage.userCard && !authority.manage.userCard.staff){
      let dd = e.detail;

      //计算明天 
      let m=parseInt(dd.month)-1;
      let d=parseInt(dd.date)+1;   
      let tomorrow=util.timeFormatDay(new Date(dd.year, m, d));
      
      this.setData({
        currentDate: dd.year + '-' + dd.month + '-' + dd.date,
        tomorrow: tomorrow
      })
  
      //获取部门日统计数据
      this.getDepartDay();
  
      //部门出勤率
      this.getRate();
    }
  },

  //获取部门日统计数据
  getDepartDay(){
    let param={
      //depart: this.data.user.depart,
      startTime: this.data.currentDate,
      endTime: this.data.tomorrow
    }

    CommonService.attendanceStatistics(param).then(res=>{
      if(res.data.data && res.data.data.result){
        let result=res.data.data.result;

        console.log('部门日统计');
        console.log(result);
        
        this.setData({
          clockInInfo: result,
          clockNum:parseInt((result.clockInPersonnelCount/result.personnelAllCount)*100),
        })
      }
    }).catch((error) => {
    })
  },
  
  //部门出勤率
  getRate(){
    let param={
      //depart: [this.data.user.depart],
      startTime: this.data.currentDate,
      endTime: this.data.tomorrow
    }

    CommonService.attendanceByDepart(param).then(res=>{
      if(res.data.data && res.data.data.result && res.data.data.result.length){
        let result=res.data.data.result;

        console.log('出勤率');
        console.log(result);

        this.setData({
          rateList: result
        })
      }
    }).catch((error) => {
    })
  },  
  
  //跳转月统计
  goToMonth(){
    wx.navigateTo({
      url: "/packageA/pages/attendanceDepartMonth/attendanceDepartMonth"
    })
  },

  //跳转打卡人员明细
  goToClockDetail({currentTarget}){
    let dataset=currentTarget.dataset;

    if(dataset.detail && dataset.detail.length){
      let obj={
        word: dataset.word,
        list: dataset.detail,
        currentDate: this.data.currentDate,
        tomorrow: this.data.tomorrow
      }
      //避免列表过长转化了再传参
      let param=encodeURIComponent(JSON.stringify(obj));
      wx.navigateTo({
        url: "/packageA/pages/attendanceDepartDetail/attendanceDepartDetail?paramObj="+param
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
